package proto

import (
	"fmt"
	"regexp"
	D "websocket_apn/define"
	"websocket_apn/log"
)

/*
71 AB 00 00 00 89 00 00 2E 7D 00 00 02 D4 07 01 81 E2 08 00
		当前积分 历史积分   剩余体力
12 68 08 D0 25 10 D0 25 18 02
22 02 08 01
22 02 08 01
22 02 08 01
22 02 08 00
22 02 08 00
22 02 08 02
22 02 08 03
22 02 08 03
22 02 08 03
22 02 08 03
22 02 08 02
22 02 08 04
22 02 08 03
22 02 08 05
22 02 08 07
22 02 08 09
2A 06 08 8B B5 06 10 00
2A 06 08 8A B5 06 10 00
2A 06 08 8D B5 06 10 02
2A 06 08 8C B5 06 10 00
	时间				32 排行
18 B4 D1 D0 D4 E8 32 20 20 28 3B

2A 06 08 8B B5 06 10 00
2A 06 08 8A B5 06 10 00
2A 06 08 8D B5 06 10 02
2A 06 08 8C B5 06 10 00
18 A4 B8 E8 D4 E8 32 20 20 28 3B
*/
//解析炼器大会信息
func ParaCastSwordInfo(dataBytes []byte) D.CastSwordInfo {
	var allInfo D.CastSwordInfo
	allInfo.Board = make([][]int, 4)
	for i := range allInfo.Board {
		allInfo.Board[i] = make([]int, 4)
	}
	if len(dataBytes) < 5 {
		return allInfo
	}
	data := BytesToHexStr(dataBytes)
	//                                   1 当前值
	re := regexp.MustCompile(`220208([0-9A-F]{2})`)
	res := re.FindAllStringSubmatch(data, -1)
	if len(res) != 16 {
		log.Errorf("解析数据失败-%s", data)
		return allInfo
	}
	for i, regRes := range res {
		allInfo.Board[i/4][i%4] = int(OneByteHexStrToByte(regRes[1]))
	}

	//                                             1 当前积分                2 历史积分                3 剩余体力
	re1 := regexp.MustCompile(`12[0-9A-F]{2}08((?:[0-9A-F]{2}){1,2})10((?:[0-9A-F]{2}){1,2})18([0-9A-F]{2})`)
	res1 := re1.FindAllStringSubmatch(data, -1)
	if len(res1) == 0 {
		log.Errorf("解析数据失败-%s", data)
	} else {
		regRes := res1[0]
		allInfo.CombatValue = ParaIntDataByHexStr(regRes[1])   //当前积分
		allInfo.Points = ParaIntDataByHexStr(regRes[2])        //历史积分
		allInfo.Strength = int(OneByteHexStrToByte(regRes[3])) //剩余体力
	}

	fmt.Println()
	for _, row := range allInfo.Board {
		for _, num := range row {
			if num == 0 {
				fmt.Printf("%4s", ".") // 用点表示空格
			} else {
				fmt.Printf("%4d", num) // 格式化输出数字
			}
		}
		fmt.Println()
	}
	fmt.Println()
	return allInfo
}
